<!--
  功能：我的预约
  作者：zzb
  邮箱：2283325110@qq.com
  时间：2021年12月06日 17:05:38
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class="">
    <el-table
      :data="tableData"
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
      style="width: 100%"
      :border="true"
      :row-style="{
        fontSize: '12px',
        color: '#666',
        fontFamily: 'MicrosoftYaHeiUI',
      }"
    >
      <el-table-column
        prop="appointDate"
        label="日期"
        width="180"
      ></el-table-column>
      <el-table-column prop="time" label="时间段" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column
        prop="whichPin"
        label="针剂"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="vaccine"
        label="疫苗"
        width="180"
      ></el-table-column>
      <el-table-column prop="appointSite" label="接种点"></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="180px">
        <template #default="{ row }">
          <el-button type="warning" @click="handleEdit(row)">修改</el-button>
          <el-button type="danger" @click="handleDelete(row)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-empty v-show="tableData[0] == null" description="暂无预约"></el-empty>
    <el-dialog
      title="修改预约"
      :visible.sync="dialogFormVisible"
      width="600px"
      @close="close"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="预约时间">
          <el-col :span="11">
            <el-date-picker
              v-model="form.appointDate"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
            ></el-date-picker>
          </el-col>
          <el-col style="text-align: center" :span="1">-</el-col>
          <el-col :span="6">
            <el-select v-model="form.time" placeholder="选择时间">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="接种针剂">
          <el-select
            v-model="form.whichPin"
            placeholder="请选择"
            size="medium"
            style="width: 340px"
          >
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接种地点">
          <el-select
            v-model="form.appointSite"
            placeholder="请选择"
            size="medium"
            style="width: 340px"
          >
            <el-option
              v-for="item in address"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接种疫苗">
          <el-select
            v-model="form.vaccine"
            placeholder="请选择"
            size="medium"
            style="width: 340px"
          >
            <el-option
              v-for="item in vaccines"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    findAppointment,
    getAppointAddress,
    getVaccines,
    modifyAppointment,
    cancelAppointment,
  } from '@/api/appoint'
  import { mapGetters } from 'vuex'

  export default {
    // 组件名称
    name: 'MyAppoint',
    // 组件参数 接收来自父组件的数据
    props: {},
    // 组件状态值
    data() {
      return {
        form: {
          appointDate: '',
          time: '',
          whichPin: '',
          appointSite: '',
          vaccine: '',
        },
        options: [
          {
            value: '上午',
            lable: '上午',
          },
          {
            value: '下午',
            lable: '下午',
          },
        ],
        options2: [
          {
            value: '第一针',
            lable: '第一针',
          },
          {
            value: '第二针',
            lable: '第二针',
          },
        ],
        address: [
          {
            value: '',
            lable: '',
          },
        ],
        vaccines: [
          {
            value: '',
            lable: '',
          },
        ],
        tableData: [],
        dialogFormVisible: false,
      }
    },
    // 计算属性
    computed: {
      ...mapGetters({
        username: 'user/username',
      }),
    },
    // 侦听器
    watch: {},
    // 以下是生命周期钩子
    /**
     * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
     */
    created() {
      this.fetchData()
      this.getAddress()
      this.getVaccines()
    },
    /**
     * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
     * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
     */
    mounted() {},
    // 组件方法
    methods: {
      close() {
        this.dialogFormVisible = false
      },
      cancel(userName) {
        cancelAppointment(userName).then((res) => {
          this.$baseMessage(res.msg, 'success')
          this.fetchData()
        })
      },
      modify(appoint) {
        modifyAppointment(appoint)
          .then((res) => {
            this.$baseMessage(res.msg, 'success')
            this.fetchData()
          })
          .catch((error) => {
            this.fetchData()
          })
      },
      save() {
        for (const key in this.form) {
          if (Object.hasOwnProperty.call(this.form, key)) {
            if (typeof this.form[key] == 'object') {
              let d = new Date(this.form[key]),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear()
              if (month.length < 2) month = '0' + month
              if (day.length < 2) day = '0' + day
              this.tableData[0][key] = [year, month, day].join('-')
            } else this.tableData[0][key] = this.form[key]
          }
        }
        this.modify(this.tableData[0])
        this.dialogFormVisible = false
      },
      async getAddress() {
        const { data } = await getAppointAddress()
        let address = []
        data.forEach((item, index) => {
          address[index] = {}
          address[index].value = item.address
          address[index].lable = item.address
        })
        this.address = address
      },
      async getVaccines() {
        const { data } = await getVaccines()
        let vaccines = []
        data.forEach((item, index) => {
          vaccines[index] = {}
          vaccines[index].value = item.name
          vaccines[index].lable = item.name
        })
        this.vaccines = vaccines
      },
      async fetchData() {
        this.tableData = []
        const { data } = await findAppointment(this.username)
        if (data == null) return
        this.tableData.push(data)
      },
      handleEdit(row) {
        for (const key in this.form) {
          if (Object.hasOwnProperty.call(this.form, key)) {
            this.form[key] = this.tableData[0][key]
          }
        }
        this.dialogFormVisible = true
      },
      handleDelete(row) {
        this.$confirm('此操作将取消该预约, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.cancel(row.userName)
            this.fetchData()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '操作取消',
            })
          })
      },
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped></style>
